package com.hpe.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 此类为数据库类
* @author TWS
*
*/
public class DBUtil {
private String url="jdbc:mysql://localhost:3306/login";
private String user="root";
private String password="";
public Connection getConnection() throws Exception{
//jdbc 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println("连接对象:"+conn);
//获取执行sql语句的对象,statement
Statement sta=conn.createStatement();
//执行sql语句,获取结果集
ResultSet rs=sta.executeQuery("select name,age from t_user");
//遍历结果集
while(rs.next()){
String name=rs.getString("name");
int age=rs.getInt("age");
System.out.println("name=" + name + ";age=" + age);
}
//关闭连接
rs.close();
sta.close();
conn.close();
return null;
}
public static void main(String[] args) throws Exception{
DBUtil dbUtil=new DBUtil();
dbUtil.getConnection();
}
}
欢迎与我分享你的看法。
转载请注明出处:http://taowusheng.cn/
微博:寒枫–0-0–
知乎:https://www.zhihu.com/people/tao-wu-sheng
豆瓣:YIFEI